package cn.ecpay.unionpay.settle.dao;

import cn.ecpay.unionpay.settle.entity.db.TPvFundSettleSingle;
import org.rex.DB;
import org.rex.db.Ps;
import org.rex.db.exception.DBException;
import org.springframework.stereotype.Repository;

import java.util.List;

import static org.rex.DB.getList;

/**
 * 单笔结算
 * Created by zhaoyanzeng on 2018/12/11.
 */
@Repository
public class SingleFundSettlementDao {

    /**
     * 插入
     * @param fundSettleSingle
     * @throws DBException
     */
    public void insert(TPvFundSettleSingle fundSettleSingle) throws DBException {
        String sql = "INSERT INTO T_PV_FUNDSETTLE_SINGLE" +
                " (ID,TXN_TYPE,TXN_NO,ACQ_INS_CODE,TXN_DATE,SND_TIME,SETTLE_METHOD,MER_ID,MER_NAME,PAYEE_BANK_NO,PAYEE_ACCT_NO,PAYEE_ACC_TYPE,PAYEE_ACCT_NAME," +
                "PAYER_ACCT_NO,PAYER_ACCT_NAME,CURRENCY_CODE,TXN_AMT,REMARK,RESP_CODE,RESP_MSG,RESP_VALIDATE,CREATE_TIME,NOTIFY_RESP_CODE," +
                "NOTIFY_RESP_MSG,IS_NOTIFY,NOTIFY_TIME,NOTIFY_VALIDATE)" +
                " VALUES" +
                " (#{id},#{txnType},#{txnNo},#{acqInsCode},#{txnDate},#{sndTime},#{settleMethod},#{merId},#{merName},#{payeeBankNo},#{payeeAcctNo}," +
                "#{payeeAccType},#{payeeAccType},#{payerAcctNo},#{payerAcctName},#{currencyCode},#{txnAmt},#{remark},#{respCode},#{respMsg}," +
                "#{respValidate},#{createTime},#{notifyRespCode},#{notifyRespMsg},#{isNotify},#{notifyTime},#{notifyValidate})";
        DB.update(sql,fundSettleSingle);
    }

    /**
     * 更新通知
     * @param fundSettleSingle
     * @throws DBException
     */
    public void updateNotify(TPvFundSettleSingle fundSettleSingle) throws DBException {
        String sql = "UPDATE T_PV_FUNDSETTLE_SINGLE SET NOTIFY_RESP_CODE=#{notifyRespCode},NOTIFY_RESP_MSG=#{notifyRespMsg},IS_NOTIFY=#{isNotify},NOTIFY_TIME=#{notifyTime},NOTIFY_VALIDATE=#{notifyValidate} " +
                    "WHERE TXN_NO=#{txnNo}";
        DB.update(sql,fundSettleSingle);
    }

    /**
     *根据ID 获取交易信息
     * @param id
     * @return
     * @throws DBException
     */
    public TPvFundSettleSingle findById(String id) throws DBException {
        String sql = "SELECT * FROM T_PV_FUNDSETTLE_SINGLE WHERE ID=?";
        return DB.get(sql,new Object[]{id},TPvFundSettleSingle.class);
    }

    /**
     *根据流水号获取交易信息
     * @param txnNo 流水号
     * @return
     * @throws DBException
     */
    public TPvFundSettleSingle findByTxnNo(String txnNo) throws DBException {
        String sql = "SELECT * FROM T_PV_FUNDSETTLE_SINGLE WHERE TXN_NO=?";
        return DB.get(sql,new Object[]{txnNo},TPvFundSettleSingle.class);
    }

    /**
     * 当天交易流水号不能重复
     * @param txnNo
     * @param txnDate
     * @return
     * @throws DBException
     */
    public List<TPvFundSettleSingle> findByTxnNo(String txnNo, String txnDate) throws DBException {
        Ps ps = new Ps();
        String sql = "SELECT * FROM T_PV_FUNDSETTLE_SINGLE WHERE TXN_NO=? and TXN_DATE =?";
        ps.add(txnNo);
        ps.add(txnDate);
        return DB.getList(sql,ps,TPvFundSettleSingle.class);
    }
}